<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\Copy;
use app\api\model\EveryTimes;
use app\Request;
use \think\middleware\Throttle;   //使用访问限制中间件Throttle
/**
 * Class Index
 * @package app\api\controller
 * 接收数据
 *
 * 需加更新：
 *          域名个数限制
 *          时间限制
 */
class Index
{

    protected $middleware = [ 
        //使用{ 访问限制中间件Throttle }再次对复制行为{addCopy}进行限制
    	Throttle::class => ['only'=> ['addCopy']]     
    ];

    /**
     * Index constructor.
     * 解决api跨域问题
     */
    public function __construct(){
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, X_Requested_With,Content-Type, Accept");
        header('Access-Control-Allow-Methods: POST,GET,PUT');
        if(request()->isOptions()){
            exit();
        }
    }


    public function index()
    {
        return '您好！这是一个[api]示例应用';
    }

    /*
     * @param Request $request
     * 接收第一次获取的信息
     * dw_user_id 用户id  ali_uId
     * dw_url   当前链接 ali_url
     * dw_ref   来源链接 ali_ref
     *      v   当前时间 unique()
     *      utid 唯一标识位  utid
     * ym_height 页面总高度  page_height
     * time  阅读时间  read_time
     * v_type  第一次类型  v_type   0是第一次进入，1是已经开始阅读
     * ym_lock 阅读最大距离 page_lock
     * domain   主域名
     * ip  访问ip
     * equipment  设备信息
     * source_type   投放类型
     *
     */
    public function everyTime(Request $request){

        $arr = [
            'user_id' =>$request->param('ali_uId'),
            'url' =>$request->param('ali_url'),
            'ref' =>$request->param('ali_ref'),
            'v' =>$request->param('ali_v'),
            'utid' =>$request->param('ali_utid'),
            'ym_height' =>$request->param('page_height'),
            'time' =>$request->param('read_time'),
            'v_type' =>$request->param('v_type'),
            'ym_lock' =>$request->param('page_lock'),
        //    'show_wx' =>$request->param('show_wx'),
            'domain' =>getUrlDomain($request->param('ali_url')),
            'ip'  =>$request->ip(),
            'equipment' =>getEquipment($_SERVER["HTTP_USER_AGENT"]),//获取设备
            'source_type'=>sourceType($request->param('ali_ref')),
            'key_word'  =>getKeyWord($request->param('ali_url'))
        ];
        $everyTimes =  new EveryTimes();

        if (!(int)$arr['v_type']){
                $address = getip($request->ip());
                $arr['province'] = $address['province'];
                $arr['city'] = $address['city'];
                $arr['show_wx'] =$request->param('show_wx');
                $everyTimes->data($arr)->save();
        }else{
            $updataFlag = $everyTimes->where(['utid'=>$arr['utid'],'user_id'=>$arr['user_id']])->find();
            if (!is_null($updataFlag)){
                $updataFlag->ym_lock = $arr['ym_lock'];
                $updataFlag->time=$arr['time'];
                $updataFlag->save();
            }
        }



        return "需要请联系QQ：894251491";

    }

    /*
     * 复制数据收集
     * @param Request $request
     * user_id 用户id  ali_uId
     * type  复制类型  type
     * url 当前url  ali_url
     * ref  来源    ali_ref
     * c   复制内容 ali_c
     * v   复制时间 ali_v
     * time   浏览时间 time
     * utid   唯一标识 utid
     * page_lock  查看页面高度 page_lock
     * page_height 页面高度 page_height
     *  domain   主域名
     * ip  访问ip
     * equipment  设备信息
     * source_type   投放类型
     */
    public function addCopy(Request $request){
        $arr = [
            'user_id' =>$request->param('ali_uId'),
            'url' =>$request->param('ali_url'),
            'ref' =>$request->param('ali_ref'),
            'show_wx' =>$request->param('show_wx'),
            'c' =>$request->param('ali_c'),
            'v' =>$request->param('ali_v'),
            'utid' =>$request->param('ali_utid'),
            'page_height' =>$request->param('page_height'),
            'time' =>$request->param('read_time'),
            'v_type' =>$request->param('copy_type'),
            'page_lock' =>$request->param('page_lock'),
            'domain' =>getUrlDomain($request->param('ali_url')),
            'ip'  =>$request->ip(),
            'equipment' => getEquipment($_SERVER["HTTP_USER_AGENT"]),//获取设备
            'source_type'=>sourceType($request->param('ali_ref')),
            'key_word'  =>getKeyWord($request->param('ali_url'))
        ];

        $copyModel = new Copy();
        $address = getip($request->ip());
        $arr['province'] = $address['province'];
        $arr['city'] = $address['city'];

         $flag = $copyModel->where(['utid'=>$request->param('ali_utid'),'ip'=>$request->ip()])->find();

        if (!is_null($flag)){
            return "需要请联系QQ：894251491";
        }

        $copyModel->data($arr)->save();
        return "需要请联系QQ：894251491";

    }


}
